package com.djk.daily.thread.background;

import java.util.concurrent.TimeUnit;

/**
 * 描述：线程安全问题，死锁
 */
public class MutiThreadError implements Runnable{
    int flag = 1;

    static Object o1 = new Object();
    static Object o2 = new Object();

    public static void main(String[] args) {
        MutiThreadError r1 = new MutiThreadError();
        MutiThreadError r2 = new MutiThreadError();
        r1.flag = 1;
        r2.flag = 0;
        new Thread(r1).start();
        new Thread(r2).start();
    }

    @Override
    public void run() {
        System.out.println("falg = " + flag);
        if (flag == 1){
            synchronized (o1){
                try {
                    TimeUnit.SECONDS.sleep(1L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){
                    System.out.println("这是flag1");
                }
            }
        }else if (flag == 0){
            synchronized (o2){
                try {
                    TimeUnit.SECONDS.sleep(1L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1){
                    System.out.println("这是flag1");
                }
            }
        }
    }
}
